package com.vmladenov.cook.ui.recipes;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.vmladenov.cook.R;
import com.vmladenov.cook.core.Helpers;
import com.vmladenov.cook.core.ParameterizedRunnable;
import com.vmladenov.cook.core.html.Recipe;
import com.vmladenov.cook.core.html.RecipeReader;

public final class ViewRecipeActivity extends Activity
{
	ProgressDialog progressDialog = null;
	Recipe recipe = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recipeview);
		ChangeControlsVisibility(View.INVISIBLE);
		recipe = null;
		recipe = (Recipe) getLastNonConfigurationInstance();
		if (recipe == null) // Initial Loading
		{
			Intent intent = getIntent();
			if (intent != null)
			{
				Bundle bundle = intent.getBundleExtra("RECIPE_ID");
				if (bundle != null)
				{
					long id = bundle.getLong("id");
					recipe = Helpers.getDataHelper().getRecipesRepository().getRecipe(id);
					ShowRecipe();
				}
				else
				{
					bundle = intent.getBundleExtra("RECIPE_URL");
					if (bundle != null)
					{
						String url = bundle.getString("url");
						LoadRecipe(url);
					}
					else
					{
						bundle = intent.getBundleExtra("RECIPE_FULL");
						recipe = new Recipe();
						recipe.setTitle(bundle.getString("title"));
						recipe.setImageUrl(bundle.getString("image"));
						recipe.setProducts(bundle.getString("products"));
						recipe.setDescription(bundle.getString("description"));
						recipe.setUserNotes("");
						recipe.setIsFavorite(false);
						ShowRecipe();
					}
				}
			}
		}
		else
		{
			ShowRecipe();
		}
	}

	Handler loadRecipeHandler = new Handler() {
		public void handleMessage(android.os.Message msg)
		{
			if (progressDialog != null) progressDialog.dismiss();
		};
	};

	private void LoadRecipe(String url)
	{
		progressDialog = ProgressDialog.show(ViewRecipeActivity.this,
				getString(R.string.loadingRecipe), getString(R.string.loading), false);

		Thread readRecipeThread = new Thread(new ParameterizedRunnable<String>(url) {

			@Override
			public void ParameterizedRun(String url)
			{
				RecipeReader reader = new RecipeReader(url);
				recipe = reader.getRecipe();
				loadRecipeHandler.post(new Runnable() {

					@Override
					public void run()
					{
						ShowRecipe();
					}
				});
				loadRecipeHandler.sendEmptyMessage(0);
			}
		});
		readRecipeThread.start();
	}

	private void ChangeControlsVisibility(int visibility)
	{
		TextView title = (TextView) findViewById(R.id.txtRecipeTitle);
		TextView products = (TextView) findViewById(R.id.txtRecipeProducts);
		TextView description = (TextView) findViewById(R.id.txtRecipeDescription);
		ImageView image = (ImageView) findViewById(R.id.imgRecipePicture);

		title.setVisibility(visibility);
		products.setVisibility(visibility);
		description.setVisibility(visibility);
		image.setVisibility(visibility);
	}

	private void ShowRecipe()
	{
		if (recipe == null) return;

		TextView title = (TextView) findViewById(R.id.txtRecipeTitle);
		TextView products = (TextView) findViewById(R.id.txtRecipeProducts);
		TextView description = (TextView) findViewById(R.id.txtRecipeDescription);
		final ImageView image = (ImageView) findViewById(R.id.imgRecipePicture);

		title.setText(recipe.getTitle());
		products.setText(recipe.getProducts());
		String desc = recipe.getDescription() + "\n" + recipe.getNotes();
		description.setText(desc);
		Helpers.setImageFromUrlAsync(image, recipe.getImageUrl());

		Boolean favorite = recipe.getIsFavorite() == null ? false : recipe.getIsFavorite();
		Boolean hasNote = recipe.getUserNotes() == null || recipe.getUserNotes().length() == 0 ? false
				: true;
		setIsFavorite(favorite);
		setHasNote(hasNote);

		ChangeControlsVisibility(View.VISIBLE);
	}

	private void setHasNote(Boolean hasNote)
	{
		ImageView image = (ImageView) findViewById(R.id.imgNotes);
		if (hasNote)
		{
			image.setImageResource(R.drawable.notes);
		}
		else
		{
			image.setImageResource(R.drawable.nonotes);
		}
	}

	private void setIsFavorite(Boolean isFavorite)
	{
		ImageView image = (ImageView) findViewById(R.id.imgFavorite);
		if (isFavorite)
		{
			image.setImageResource(R.drawable.remove_favorite);
		}
		else
		{
			image.setImageResource(R.drawable.add_favorite);
		}
	}

	@Override
	public Object onRetainNonConfigurationInstance()
	{
		return recipe;
	}

	public void onFavoriteClick(View view)
	{
		if (recipe != null && recipe.getId() > 0)
		{
			recipe.setIsFavorite(!recipe.getIsFavorite());
			setIsFavorite(recipe.getIsFavorite());
			if (recipe.getIsFavorite())
			{
				Helpers.getDataHelper().getRecipesRepository().addFavorite(recipe.getId());
			}
			else
			{
				Helpers.getDataHelper().getRecipesRepository().removeFavorite(recipe.getId());
			}
		}
	}

	public void onViewNotes(View view)
	{
		if (recipe != null && recipe.getId() > 0)
		{
			final AlertDialog alertDialog;

			LayoutInflater inflater = (LayoutInflater) this
					.getSystemService(LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.recipenotes,
					(ViewGroup) findViewById(R.id.layout_root));

			final EditText edit = (EditText) layout.findViewById(R.id.txtEditNote);
			edit.setText(recipe.getUserNotes());

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setView(layout);

			builder = new AlertDialog.Builder(this);
			builder.setView(layout);
			alertDialog = builder.create();
			Button button = (Button) layout.findViewById(R.id.bOk);
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v)
				{
					recipe.setUserNotes(edit.getText().toString());
					setHasNote(recipe.getUserNotes().length() > 0);
					Helpers.getDataHelper().getRecipesRepository()
							.setRecipeNote(recipe.getId(), recipe.getUserNotes());
					alertDialog.dismiss();
				}
			});
			alertDialog.show();
		}
	}

	public void onShare(View v)
	{
		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(Intent.EXTRA_SUBJECT, recipe.getTitle());
		shareIntent.putExtra(Intent.EXTRA_TEXT, recipe.getUrl());
		startActivity(Intent.createChooser(shareIntent, "ShareWindowTitle"));
	}
}
